﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Finance;
using AMM.Model;
using AMM.Model.Finance;

namespace AMM.Business.Finance {
    public class CostProcessor : BaseProcessor {

        private int _pageSize;
        private CostRepository _costRepository;

        public CostProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._costRepository = new CostRepository(pageSize);
        }

        public Cost FetchCostById(int costId) {
            try {
                return this._costRepository.FetchCostById(costId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定收支记录失败！");
            }
        }

        public List<Cost> FetchCostList() {
            try {
                return this._costRepository.FetchCostList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取收支记录失败！");
            }
        }

        public List<CostViewModel> FetchCostViewModelList(CostSearchModel searchModel, int pageIndex = 1) {
            try {
                List<Cost> resultList = this._costRepository.FetchCostList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new CostViewModel() {
                        Key = x.CostId,
                        AccountNumber = x.AccountNumber,
                        Amount = x.Amount,
                        CostType = x.CostType == 1 ? "收入" : "支出",
                        StatementCode = (x.Statement ?? new Statement()).Code,
                        Remarks = x.Remarks,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取收支记录失败！");
            }
        }

        public void Insert(Cost cost) {
            try {
                cost.CostId = Guid.NewGuid().GetHashCode();
                cost.CreatedDatetime = DateTime.Now;
                this._costRepository.Insert(cost);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新收支记录失败！");
            }
        }

        public void Update(Cost cost) {
            try {
                Cost oldCost = this.FetchCostById(cost.CostId);
                if (!oldCost.RowVersion.SequenceEqual(cost.RowVersion)) {
                    throw this.BuildException("更新收支记录失败，数据不一致性错误！");
                }
                cost.ModifiedDatetime = DateTime.Now;
                this._costRepository.Update(cost);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新收支记录失败！");
            }
        }

        public void Delete(Cost cost) {
            try {
                this._costRepository.Delete(cost);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除收支记录失败！");
            }
        }

        public void Delete(params int[] costIds) {
            try {
                this._costRepository.DeleteMultipleCost(costIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除收支记录失败！");
            }
        }
    }
}
